package com.zyp.dao.imp;



import com.zyp.bean.User;
import com.zyp.dao.UserDao;
import com.zyp.ubil.DBUtbil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//实现类
public class UserDaoImp implements UserDao {

    @Override
    public User userLogin(User user) {
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement ps = null;
        User tu = null;
        try {
            //1.获取数据库连接
            connection = DBUtbil.getConnection();
            //2.准备sql语句
            String sql = "select * from t_user where userName=? and password=?";
            //3.执行sql语句后获取结果并且返回
            ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassword());
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                tu = new User(resultSet.getString("userName"), resultSet.getString("password"));
                return tu;//查找成功
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.释放相关资源
            try {
                DBUtbil.closeResource(connection, ps);
                if (null != resultSet) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;//查找失败
    }

}